package com.fanshilz.flower2025.apps.openapi.controller;

import cn.hutool.core.map.MapUtil;
import com.fanshilz.flower2025.apps.admin.sysRole.resp.SelectResp;
import com.fanshilz.flower2025.apps.openapi.req.AddressSearchReq;
import com.fanshilz.flower2025.apps.openapi.req.DictTypeListReq;
import com.fanshilz.flower2025.business.sys.sysDictItem.entity.SysDictItem;
import com.fanshilz.flower2025.business.sys.sysDictItem.service.ISysDictItemService;
import com.fanshilz.flower2025.framework.aliyun.sts.AliyunSts;
import com.fanshilz.flower2025.framework.aliyun.sts.AliyunStsUtil;
import com.fanshilz.flower2025.framework.resp.Resp;
import com.fanshilz.flower2025.framework.util.address.AddressUtil;
import com.fanshilz.flower2025.framework.util.address.MapAddress;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.fanshilz.flower2025.business.sys.sysDictItem.entity.table.SysDictItemTableDef.SYS_DICT_ITEM;

@Tag(name = "公开Api")
@RestController
@RequestMapping("/openapi")
public class OpenApiController {

    @Resource
    private ISysDictItemService sysDictItemService;

    @Operation(summary = "地址检索 弃用")
    @PostMapping("/address/search")
    public Resp<List<MapAddress>> search(@RequestBody @Valid AddressSearchReq req) {
//        return Resp.success(AddressUtil.addresses);
        return Resp.success();
    }

    @Operation(summary = "字典类型检索")
    @PostMapping("/dictTypeList")
    public Resp<Map<String, List<SelectResp>>> dictTypeList(@RequestBody @Valid DictTypeListReq req) {

        List<SysDictItem> list = sysDictItemService.list(QueryWrapper.create()
                .where(SYS_DICT_ITEM.DICT_KEY.in(req.getKeys())));

        Map<String, List<SysDictItem>> group = list.stream().collect(Collectors.groupingBy(SysDictItem::getDictKey));

        Map<String, List<SelectResp>> resp = MapUtil.newHashMap();
        group.forEach((k, v) -> resp.put(k, v.stream().map(di -> new SelectResp(di.getDictItemLabel(), Long.valueOf(di.getDictItemValue()))).toList()));

//        List<SysDictItem> list = sysDictItemService.list(QueryWrapper.create()
//                .where(SYS_DICT_ITEM.DICT_KEY.in(req.getKeys())));
//        List<SelectResp> resp = list.stream().map(di -> new SelectResp(di.getDictItemLabel(), Long.valueOf(di.getDictItemValue()))).toList();
        return Resp.success(resp);
    }

}
